package wwk.common.phone;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.WindowManager;
import android.widget.TextView;

public class SuspensionWindow {

	private WindowManager windowManager;
	private TextView textView;
	private WindowManager.LayoutParams params;
	
	@SuppressWarnings("deprecation")
	public void showText(Context context,String text,Drawable background)
	{
		hide();
		
		windowManager = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);    
		
		if(params==null)
		{
			params = new WindowManager.LayoutParams();    
	        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;    
	        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;     
	        params.width = WindowManager.LayoutParams.WRAP_CONTENT;    
	        params.height = WindowManager.LayoutParams.WRAP_CONTENT;    
	        params.format = PixelFormat.RGBA_8888;  
	        params.verticalMargin=0.07f;
		}
        
        textView = new TextView(context);
        if(background!=null)
        {
        		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
	        {
        			textView.setBackground(background);
	        }
        		else
        		{
        			textView.setBackgroundDrawable(background);
        		}
        }
        else
        {
        		textView.setBackgroundColor(Color.BLACK);
        }
        textView.setTextColor(Color.WHITE);
        textView.setText(text);  
        
        windowManager.addView(textView, params);  
	}
	
	public void hide()
	{
		if(windowManager!=null && textView!=null)
		{
			windowManager.removeViewImmediate(textView);
		}
		
		windowManager=null;
		textView=null;
	}
}
